<?php include '../public/header.php' ?>
<?php include '../public/header_index.php' ?>
<body class="laytp-container">
<div id="opt_nav">
</div>

<div class="layui-card" id="search-form">
    <div class="layui-card-body">
        <form class="layui-form" id="TableSearchFormId" lay-filter="TableSearchForm" onkeydown="keyTableSearch('TableSearchForm', 'datalist')">
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-md3 layui-hide yiban" id="wenname">
                        <label class="layui-form-label">文件名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="wenname" placeholder="请输入" class="layui-input" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-col-md3 yiban">
                        <label class="layui-form-label">项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" placeholder="请输入" class="layui-input" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-col-md3 yiban">
                        <label class="layui-form-label">项目所属单位</label>
                        <div class="layui-input-block">
                            <div id="chanid" class="xmselect_div">
                                <div class="xmselect_title">请选择</div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md3 yiban">
                        <label class="layui-form-label">项目属性：</label>
                        <div class="layui-input-block">
                            <select id="business" name="business" lay-filter="business">
                                <option value="">请选择</option>
                                <option value="1">自营</option>
                                <option value="2">联营</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-col-md3 search_hide none yiban">
                        <label class="layui-form-label">项目类型</label>
                        <div class="layui-input-block">
                            <div id="engintype" class="xmselect_div">
                                <div class="xmselect_title">请选择</div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md3 search_hide none yiban">
                        <label class="layui-form-label">项目状态：</label>
                        <div class="layui-input-block">
                            <select id="state" name="state" lay-filter="state">
                                <option value="">请选择</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-col-md3">
                        <div class="layui-form-item search-div">
                            <div class="layui-form-item layui-inline">
                                <span id="fanbut" class="layui-hide" enginid="0" curpid="0" shangpid="0"></span>
                                <button class="laytp-btn laytp-btn-md laytp-btn-primary" id="TableSearchButton" type="button"><i class="layui-icon layui-icon-search"></i>查询</button>
                                <button class="layui-btn layui-btn-primary" onclick="listTableReset()" id="TableSearchReset" type="reset" ><i class="layui-icon layui-icon-refresh"></i>重置</button>
                                <div id="search_open" onclick="toggle_search()">
                                    <i class="layui-icon layui-icon-down"></i>展开
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="layui-card" id="table-card">
    <div class="layui-card-body" id="listfold">
        <div class="layui-row">
            <div class="layui-col-md12" id="table_list">
                <div class="layui-card-body">
                    <table id="datalist" lay-filter="datalist"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="test-dropdown-toolbar-barDemo-engin">
    <div class="tab-list-authbut">
        <a href="javascript:;" class="bar_opt" onclick="auth_but_func_show({{d.id}})">进入文件夹</a>
    </div>
</script>
<script type="text/html" id="test-dropdown-toolbar-barDemo">
    <div class="tab-list-authbut">
    </div>
</script>
<script type="text/html" id="toolbarDemo">
    <div style="float: left">
        <span id="fanbut_anniu" class="layui-hide">
            <button class="layui-btn layui-btn-primary" onclick="auth_but_func_fanbut()">
                <i class="layui-icon layui-icon-return"></i>返回上一层
            </button>
        </span>
    </div>
    <div class = "layui-btn-container" style="padding-top: 5px;padding-left: 140px"></div>

</script>
<script>

    var EnginViewerObj;
    function upload_engin_files_file_viewer(dom='listfold'){
        try {
            EnginViewerObj.destroy();
        }catch (e) {

        }
        EnginViewerObj = new Viewer(document.getElementById('listfold'));
    }
    function upload_engin_files_viewer_img_show(index=0){
        EnginViewerObj.view(index);
    }


    var currButAuth, currTabData, fileArrs,filenum=0;
    initEnginTable();
    function initEnginTable(){

        $(".yiban").removeClass('layui-hide');
        $("#wenname").addClass('layui-hide');

        layui.use(["laytp",'laypage','table','dropdown','form','soulTable'], function () {
            var laypage = layui.laypage, table = layui.table, dropdown = layui.dropdown, form = layui.form, soulTable = layui.soulTable;

            $('#TableSearchReset').click(function () {
                $('.xmselect_div>xm-select').find('.xm-label-block').css('display','none');
                $('.xmselect_div').append('<div class="xmselect_title">请选择</div>');
                searchTableFunc(form, table)
            });


            setSouTableExport('项目资料库');
            var mytable = table.render({
                elem: "#datalist",id: "datalist"
                ,url: facade.url(ask_engin_fold_list_engin),where: {}, page: true, height: 'full-140'
                ,soulSort: TabInit.soulSort,method: TabInit.method, toolbar: TabInit.toolbar, overflow: TabInit.overflow
                ,defaultToolbar: TabInit.defaultToolbar,filter: TabInit.filter
                ,request: TabInit.request,response: TabInit.response,contextmenu:get_table_contextmenu(soulTable)
                ,text: TabInit.text
                ,parseData: function (res) {


                    xmselect_dan('chanid', res.data.chanid, res.data.is_chanid, function (res) {
                        setTimeout(function () {
                            searchTableFunc(form, table);
                        },100)
                    })
                    xmselect_dan('engintype', res.data.engintype, res.data.is_engintype, function (res) {
                        setTimeout(function () {
                            searchTableFunc(form, table);
                        },100)
                    })




                    var state_str = '<option value="">请选择</option>';
                    var state = res.data.state;
                    for (var key in state){
                        if(res.data.is_state == key){
                            state_str += '<option value="'+key+'" selected>'+state[key]+'</option>';
                        }else {
                            state_str += '<option value="'+key+'">'+state[key]+'</option>';
                        }

                    }
                    $("#state").html(state_str);



                    render_from();
                    return {
                        "code": res.code,
                        "msg": res.message,
                        "count": res.data.data.total,
                        "data": res.data.data.data
                    };
                }
                , cols: [[
                    {type: "checkbox", fixed: 'left'}
                    ,{type:'numbers',title:'序号',fixed:'left'}
                    ,{field: 'state_name', title: '项目状态'}
                    ,{field:'name',title:'项目名称',templet: function(d){
                            return '<span class="colorlan" onclick="openPage(\'../engin/show.php?id='+d.id+'\')">'+d.name+'</span>';
                        }}
                    ,{field: 'business_name', title: '项目属性'}
                    ,{field: 'filenums', title: '文件数量'}
                    ,{field: 'number', title: '项目编号'}
                    ,{field: 'engintype', title: '项目类型'}
                    ,{fixed: 'right', title:'操作', toolbar: '#test-dropdown-toolbar-barDemo-engin',align:'center',width:200}
                ]]
                , done: function (res) {
                    soulTable.render(this);
                }
            });


            listTableSearch(form, table);
        });
    }

    function initFoldTable(enginid=0, pid=0){
        $(".yiban").addClass('layui-hide');
        $("#wenname").removeClass('layui-hide');

        layui.use(["laytp",'laypage','table','dropdown','form','soulTable'], function () {
            var laypage = layui.laypage, table = layui.table, dropdown = layui.dropdown, form = layui.form, soulTable = layui.soulTable;

            setSouTableExport('项目资料库');
            var mytable = table.render({
                elem: "#datalist",id: "datalist"
                ,url: facade.url(ask_engin_fold_list),where: {enginid:enginid, pid:pid}, page: true
                ,soulSort: TabInit.soulSort,method: TabInit.method, toolbar: TabInit.toolbar, overflow: TabInit.overflow
                ,defaultToolbar: TabInit.defaultToolbar,filter: TabInit.filter
                ,request: TabInit.request,response: TabInit.response,contextmenu:get_table_contextmenu(soulTable)
                ,parseData: function (res) {
                    currButAuth = res.data.butauth;
                    tableButShow(currButAuth);

                    $("#fanbut").attr('curpid', res.data.curpid);
                    $("#fanbut").attr('shangpid', res.data.shangpid);
                    $("#fanbut_anniu").removeClass('layui-hide');

                    fileArrs = [];
                    filenum = 0;

                    render_from();
                    return {
                        "code": res.code,
                        "msg": res.message,
                        "count": res.data.data.total,
                        "data": res.data.data.data
                    };
                }
                , cols: [[
                    {type: "checkbox", fixed: 'left'}
                    ,{type:'numbers',title:'序号',fixed:'left'}
                    ,{field:'name',title:'文件名称',event:"wenjianjia",templet: function (d) {
                        if(d.type > 0){
                            if(d.ext == 'png' || d.ext == 'pneg' || d.ext == 'jpg' || d.ext == 'jpeg' || d.ext == 'gif'){
                                fileArrs[d.id] = filenum;
                                filenum++;
                                return '<a class="colorcheng">'+d.name+'</a><img src="'+get_engin_file_complete(d.encpath)+'" style="display: none">';
                            }else{
                                return '<a class="colorcheng">'+d.name+'</a>';
                            }

                        }else{
                            return '<a class="colorlan">'+d.name+'</a>';
                        }
                    }}
                    ,{field: 'created_user_name', title: '新建人'}
                    ,{field: 'created_at', title: '新建时间'}
                    ,{fixed: 'right', title:'操作', toolbar: '#test-dropdown-toolbar-barDemo',align:'center',width:200}
                ]]
                , done: function (res) {
                    soulTable.render(this);

                    currTabData = res.data;
                    $.each(currTabData, function(index,item){
                        var chk = $('tr[data-index=' + item.LAY_TABLE_INDEX + '] .tab-list-authbut');
                        for (var key in currButAuth) {
                            if(currButAuth[key].pagetype === 0 || currButAuth[key].pagetype === 1){
                                if(currTabData[index].type < 1){
                                    if(currButAuth[key].butname == 'down' || currButAuth[key].butname == 'move'){
                                        continue;
                                    }
                                }else{
                                    if(currButAuth[key].butname == 'editjia'){
                                        continue;
                                    }
                                }

                                chk.append(' <a ' +
                                    'href="javascript:;" ' +
                                    'class="bar_opt layui-hide" ' +
                                    'lay-event="search" ' +
                                    'data-butname="'+currButAuth[key].butname+'" ' +
                                    'data-title="'+currButAuth[key].title+'" ' +
                                    'onclick="auth_but_func_'+currButAuth[key].butname+'(\''+currButAuth[key].title+'\', '+index+')"' +
                                    '>'+currButAuth[key].title+'' +
                                    '</a>');
                            }
                        }
                    });
                    show_tab_but(currTabData);
                    upload_engin_files_file_viewer();
                }
            });

            table.on('tool(datalist)',function (obj) {
                var data = obj.data;
                if(obj.event === 'wenjianjia'){
                    if(data.type > 0){
                        var fileext = data.ext;
                        var fileurl = get_engin_file_complete(data.encpath);
                        if(fileext == 'png' || fileext == 'pneg' || fileext == 'jpg' || fileext == 'jpeg' || fileext == 'gif'){
                            upload_engin_files_viewer_img_show(fileArrs[data.id]);
                        }else if(fileext == 'pdf'){
                            upload_files_viewer_pdf_show(fileurl);
                        }else if(fileext == 'doc' || fileext == 'docx' || fileext == 'xls' || fileext == 'xlsx' || fileext == 'ppt' || fileext == 'pptx'){
                            upload_files_vieweroffice_show(fileurl);
                        }else{
                            facade.error("此文件类型不支持预览,请下载查看");
                        }
                    }else{
                        var enginid = $("#fanbut").attr('enginid');
                        initFoldTable(enginid, data.id);
                    }
                }
            });

            listTableSearch(form, table);
        });
    }


    function auth_but_func_fanbut(){
        var curpid = parseInt($("#fanbut").attr('curpid'));
        var shangpid = parseInt($("#fanbut").attr('shangpid'));
        var enginid = parseInt($("#fanbut").attr('enginid'));

        if(curpid == 0){
            $("#fanbut").attr('curpid',-1).attr('shangpid',-1);
            $("#fanbut_anniu").addClass('layui-hide');
            $("#tab-init-auth-but-out").html('');
            initEnginTable();
        }
        if(curpid > 0){
            initFoldTable(enginid, shangpid);
        }
    }

    function auth_but_func_show(id){
        $("#fanbut").attr('enginid', id);
        initFoldTable(id);
    }
    function auth_but_func_addjia(){
        var pid = parseInt($("#fanbut").attr('curpid'));
        var enginid = parseInt($("#fanbut").attr('enginid'));
        openPageEdit('addjia.php?close=1&enginid='+enginid+'&pid='+pid, '770px', '490px');
    }
    function auth_but_func_addfile(){
        var pid = parseInt($("#fanbut").attr('curpid'));
        var enginid = parseInt($("#fanbut").attr('enginid'));
        if(pid < 1){
            facade.error("请先选择要上传的文件夹");return false;
        }

        layui.use('layer',function(){
            var layer = layui.layer;
            layer.open({
                type: 2,
                title: '上传文件',
                maxmin: true,
                area: ['770px', '490px'],
                content: 'addfile.php?enginid='+enginid+'&pid='+pid,
                cancel :function (index) {
                    layer.confirm("正在上传文件信息，是否直接关闭窗口？",function (conindex) {
                        layer.close(conindex);
                        layer.close(index);
                    });
                    return false;
                }
            });
        });
    }
    function auth_but_func_editjia(buttitle, index){
        var trData = currTabData[index];
        openPageEdit('editjia.php?close=1&id='+trData.id, '770px', '490px');
    }
    function auth_but_func_down(buttitle, index){
        var trData = currTabData[index];
        public_down_engin_file(trData.encpath)
    }
    function auth_but_func_move(buttitle, index){
        var trData = currTabData[index];
        openPageEdit('move.php?close=1&id='+trData.id, '770px', '490px');
    }
    function auth_but_func_delete(buttitle, index){
        var trData = currTabData[index];
        data_delete(ask_engin_fold_del, {id:trData.id});
    }

</script>
</body>
</html>
